﻿using System.Collections.Generic;

using System.Web;
using com.byt.webcore;
using com.byt.webcore.utils;
using MYLSOFT.JsonSimple;
using com.byt.suce.db.mapper;
using com.byt.suce.db.factory;
using System.Collections;
using System.Data.Common;
using System.Text;
using System.IO;
using System.Xml;
using com.byt.suce.db.dao;
using System;

namespace suce.gestor
{

    public class Servicio : ServletBase
    {

        private IPkgGestorServicio DAO = PkgGestorServicioFactory.create();
        private IPkgSolicitud DAOsol = PkgSolicitudFactory.create();

        protected override void doCommand(com.byt.webcore.ServletBase.Parameters p)
        {
            if ("ORDEN".Equals(p.command))
                ORDEN(p);
            else if ("SUCE".Equals(p.command))
                SUCE(p);
            else if ("SUBSANACION".Equals(p.command))
                SUBSANACION(p);
            else if ("N".Equals(p.command))
                N(p);

        }

        private void ORDEN(Parameters p)
        {
            if ("ACEPTAR".Equals(p.subcommand))
            {
                PkgVuce.N2Param param = PkgVuceFactory.create().n2(p.conn, p.getMap());
            }
            else if ("GETTASA".Equals(p.subcommand))
            {
                PkgVuce.N2TarifaParam param = PkgVuceFactory.create().n2Tarifa(p.conn, p.getMap());
                p.json.Add("tasa", param.Tasa);
            }

        }

        private void SUCE(Parameters p)
        {


            if ("ASIGNAREVALUADOR".Equals(p.subcommand))
            {
                PkgVuce.N57Param param = PkgVuceFactory.create().newN57Param(p.getMap());
                param.Iduserevaluador = p.iduser;
                PkgVuceFactory.create().n57(p.conn, param);

            }
            else if ("RESOLVER".Equals(p.subcommand))
            {
                PkgVuce.N7ElabDrParam suceResolver = PkgVuceFactory.create().newN7ElabDrParam(p.getMap());
                suceResolver.Iduserevaluador = p.iduser;
                PkgVuceFactory.create().n7ElabDr(p.conn, suceResolver);

            }
            else if ("APROBAR".Equals(p.subcommand))
            {
                PkgVuce.N8Param n8 = PkgVuceFactory.create().newN8Param(p.getMap());    // recoger parametros web
                PkgVuceFactory.create().n8(p.conn, n8);
            }
            else if ("DENEGAR".Equals(p.subcommand))
            {
                PkgVuce.N9Param n9 = PkgVuceFactory.create().newN9Param(p.getMap());    // recoger parametros web
                PkgVuceFactory.create().n9(p.conn, n9);

            }
            else if ("DEVOLVER".Equals(p.subcommand))
            {
                PkgVuce.N7DevolverParam n7dev = PkgVuceFactory.create().newN7DevolverParam(p.getMap()); // recoger parametros web
                PkgVuceFactory.create().n7Devolver(p.conn, n7dev);

            }
        }

        private void SUBSANACION(Parameters p)
        {

            if ("GENERAR".Equals(p.subcommand))
            {
                //SOL_subsana_update
                PkgSolicitud.SubsanaUpdateParam param = DAOsol.subsanaUpdate(p.conn, p.getMap());
                
                //VUC_N11_CREAR
                PkgVuce.N11CrearParam n11 = PkgVuceFactory.create().newN11CrearParam(p.getMap());
                n11.Idsubsanacion = param.Idsubsanacion;
                PkgVuceFactory.create().n11Crear(p.conn, n11);

            }
            else if ("CREAR".Equals(p.subcommand))
            {
                PkgSubsanacion.CrearParam param = PkgSubsanacionFactory.create().crear(p.conn, p.getMap());
                p.print(Utils.getJSONObject(param.CurOut));
                param.CurOut.Close();

            }
            else if ("ENVIAR".Equals(p.subcommand))
            {

                Connection vuce = null;
                try
                {
                    // Empaquetar los adjuntos

                    // Enviar a la VUCE
                    vuce = new Connection(AppConfig.getConnection(p, "integrador"));
                    vuce.BeginTransaction();
                    PkgGestorServicio.OrdenSubsanarParam ordenSub = DAO.newOrdenSubsanarParam();
                    ordenSub.Numdocumento = p.getParameter("numesolicitud");
                    ordenSub.Texto = p.getParameter("descsubsanacion");
                    DAO.ordenSubsanar(vuce, ordenSub);

                    // Crear el movimiento en GestorSuce
                    PkgVuce.N11Param n11 = PkgVuceFactory.create().newN11Param(p.getMap());
                    n11.Idsubsanacion = Utils.parseInteger(ordenSub.Idnotificacion);
                    PkgVuceFactory.create().n11(p.conn, n11);

                }
                catch { if (vuce != null) vuce.Rollback(); throw; }
                finally { if (vuce != null) vuce.Close(); }

            }
            else if ("CANCELAR".Equals(p.subcommand))
            {
                PkgSubsanacionFactory.create().cancelar(p.conn, p.getMap());

            }

        }

        private void N(Parameters p)
        {

            if ("N2".Equals(p.subcommand))
            {
                PkgVuceFactory.create().n2(p.conn, p.getMap());
            }
            else if ("N8".Equals(p.subcommand))
            {
                PkgVuceFactory.create().n8(p.conn, p.getMap());
            }
            else if ("N9".Equals(p.subcommand))
            {
                PkgVuceFactory.create().n9(p.conn, p.getMap());
            }
            else if ("N11".Equals(p.subcommand))
            {
                PkgVuceFactory.create().n11(p.conn, p.getMap());
            }
            else if ("N15".Equals(p.subcommand))
            {
                PkgVuceFactory.create().n15(p.conn, p.getMap());
            }
            else if ("N17".Equals(p.subcommand))
            {
                PkgVuceFactory.create().n17(p.conn, p.getMap());
            }
            else if ("N17GET".Equals(p.subcommand))
            {
                PkgVuce.N17GetParam param = PkgVuceFactory.create().n17Get(p.conn, p.getMap());
                p.json.Add("idsubsanacion", param.Idsubsanacion);

            }
            else if ("N17N66".Equals(p.subcommand))
            {
                PkgVuce.N17N66Param param = PkgVuceFactory.create().n17N66(p.conn, p.getMap());

            }
            else if ("N19".Equals(p.subcommand))
            {
                PkgVuceFactory.create().n19(p.conn, p.getMap());
            }
            else if ("N21".Equals(p.subcommand))
            {
                PkgVuceFactory.create().n21(p.conn, p.getMap());
            }
            else if ("N22".Equals(p.subcommand))
            {
                PkgVuceFactory.create().n22(p.conn, p.getMap());
            }
            else if ("N36".Equals(p.subcommand))
            {
                PkgVuceFactory.create().n36(p.conn, p.getMap());
            }
            else if ("N39".Equals(p.subcommand))
            {
                PkgVuceFactory.create().n39(p.conn, p.getMap());
            }
            else if ("N48".Equals(p.subcommand))
            {
                PkgVuceFactory.create().n48(p.conn, p.getMap());
            }
            else if ("N49".Equals(p.subcommand))
            {
                PkgVuceFactory.create().n49(p.conn, p.getMap());
            }
            else if ("N57".Equals(p.subcommand))
            {
                PkgVuceFactory.create().n57(p.conn, p.getMap());
            }
            else if ("N66".Equals(p.subcommand))
            {
                PkgVuceFactory.create().n66(p.conn, p.getMap());
            }

        }

    }
}